﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using DataModel;
using A_ReferenceCodes.Services.Admin.RefCodes;
namespace A_ReferenceCodes
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            //ObservableCollection<TabItem> Tabs = new ObservableCollection<TabItem>();
            RCC.GetCodeTypesCompleted += new EventHandler<GetCodeTypesCompletedEventArgs>(RCC_GetCodeTypesCompleted);
            RCC.GetCodeTypesAsync();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            NewRefCodeCommand.ParentRoot = LayoutRoot;
        }


        void RCC_GetCodeTypesCompleted(object sender, GetCodeTypesCompletedEventArgs e)
        {
            //((MutableString)Resources["MetaDataTitle"]).Value = "Hello Again";
            this.DataContext = e.Result.Select(SelectCodeTypeDM).Select(SelectTabItem);
        }

        DM_CodeTypeBase SelectCodeTypeDM(CodeType CT)
        {
            return DM_CodeTypeBase.GetCodeType(CT);
        }
        TabItem SelectTabItem(DM_CodeTypeBase CT)
        {
            return new TabItem()
            {
                Content = CT,
                Header = CT,
                ContentTemplate = ContentT,
                HeaderTemplate = HeaderT
            };
        }














        RefCodesServiceClient RCC = ServiceConfig.NewRefCodesService();


        private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            var DG = ((DataGrid)sender);
            UpdateColumns(DG);
            //DG = ((DataGrid)sender);
            //const int MetaDataColumn = 3;
            //((DataGrid)sender).Columns[MetaDataColumn].Visibility = ((DM_CodeTypeBase)((DataGrid)sender).DataContext).IsMetaDataVisible;
            //if (((DataGrid)sender).Columns[MetaDataColumn].Visibility == System.Windows.Visibility.Visible)
            //    ((DataGrid)sender).Columns[MetaDataColumn].Header = ((DM_CodeTypeBase)((DataGrid)sender).DataContext).MetaDataTitle;
        }
        void UpdateColumns(DataGrid DG)
        {
            if (DG == null)
                return;
            const int MetaDataColumn = 3;
            DG.Columns[MetaDataColumn].Visibility = ((DM_CodeTypeBase)(DG).DataContext).IsMetaDataVisible;
            if (DG.Columns[MetaDataColumn].Visibility == System.Windows.Visibility.Visible)
                DG.Columns[MetaDataColumn].Header = ((DM_CodeTypeBase)DG.DataContext).MetaDataTitle;

            //DG.InvalidateArrange();
        }
    }
    public class NewRefCodeCommand:ICommand
    {
        public static Panel ParentRoot;
        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged { add { } remove { } }

        public void Execute(object parameter)
        {
            DM_CodeTypeBase CTB = (DM_CodeTypeBase)parameter;
            NewRefCode NRC = new NewRefCode(CTB);
            NRC.ParentLayoutRoot = ParentRoot;
            NRC.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(NRC_Closing);
            NRC.ShowDialog();

        }

        void NRC_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var OldNRC = (NewRefCode)sender;
            if (OldNRC.DialogResult == true)
            {
                NewRefCode NRC = new NewRefCode(OldNRC.CT);
                NRC.ParentLayoutRoot = ParentRoot;
                NRC.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(NRC_Closing);
                NRC.ShowDialog();
            }
        }

        #endregion
    }
}
